﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;
using System.Text;

namespace CoolERP_Web.User
{
    public partial class SystemParam_Query : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AspNetPager1.PageSize = GridView1.PageSize;
                BindData();
            }
        }

        #region=======GridView处理=======
        private void BindData()
        {
            using (SystemParamBLL sBLL = new SystemParamBLL())
            {
                int RecordCount = 0;
                List<SystemParam> itemLists = sBLL.GetPagerList(null, "key asc", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.RecordCount = RecordCount;
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void GridView1_PreRender(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count == 0)
                Utility.RenderEmptyGridView(GridView1, "key,value,bz");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridView1.ShowFooter = false;
            BindData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int result = 0;
            string key = GridView1.DataKeys[e.RowIndex].Value.ToString();
            //TextBox txtKey = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtKey"));
            TextBox txtValue = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtValue"));
            TextBox txtBZ = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtBZ"));
            using (SystemParamBLL cBLL = new SystemParamBLL())
            {
                SystemParam item = cBLL.GetEntity(t => t.key == key);
                //item.key = txtKey.Text.Trim();
                item.value = txtValue.Text.Trim();
                item.bz = txtBZ.Text.Trim();
                result = cBLL.SaveEntity(item, null);
            }
            if (result == 1)
                ShowMsg("保存成功");
            else
                ShowMsg("保存失败！已存在此键，请重新输入");
        }

        #endregion

        #region=======添加类型=======
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.EditIndex = -1;
            GridView1.ShowFooter = true;
            BindData();
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            List<SystemParam> items = new List<SystemParam>();
            SystemParam item = new SystemParam();
            try
            {
                StringBuilder sbIds = new StringBuilder();
                foreach (GridViewRow container in GridView1.Rows)
                {
                    HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                    if (cBox != null && cBox.Checked)
                        sbIds.Append(string.Format(",{0}", cBox.Value));
                }
                if (sbIds.Length > 0)
                {
                    sbIds = sbIds.Remove(0, 1);
                    using (SystemParamBLL rBLL = new SystemParamBLL())
                    {
                        if (rBLL.Deletes("key", sbIds.ToString()))
                            this.ShowMessage("删除成功");
                        else
                            this.ShowMessage("删除失败");
                    }
                }
            }
            catch (Exception ex)
            {
                this.ShowMessage("删除失败\n" + ex.Message);

            }
            finally
            {
                BindData();
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            int result = 0;
            TextBox txtKey = GridView1.FooterRow.FindControl("txtKey") as TextBox;
            TextBox txtValue = GridView1.FooterRow.FindControl("txtValue") as TextBox;
            TextBox txtBZ = GridView1.FooterRow.FindControl("txtBZ") as TextBox;
            if (!string.IsNullOrEmpty(txtKey.Text.Trim())||!string.IsNullOrEmpty(txtValue.Text.Trim()))
            {
                using (SystemParamBLL cBLL = new SystemParamBLL())
                {
                    SystemParam item = new SystemParam();
                    item.key = txtKey.Text.Trim();
                    item.value = txtValue.Text.Trim();
                    item.bz = txtBZ.Text.Trim();
                    result = cBLL.SaveEntity(item, null);
                }
                if (result == 1)
                    ShowMessage("保存成功");
                else
                    ShowMessage("保存失败，已存在此键，请重新输入");
                BindData();
            }
            else
            {
                ShowMessage("键和值都不能为空，请确认无误后再保存！");
                return;
            }
        }

        protected void btnCancel_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = false;
            BindData();
        }
        #endregion
    }
}